//
//  LiveView.swift
//  BaoDan
//
//  Created by 郭子龙 on 2025/4/13.
//

import BDUIKit
import UIKit

struct LiveData: Codable {
  let liveTopic: String  // 直播标题
  let liveStatus: Int  //  1-待开播,2-直播中, 3-已结束,4-已过期
  let liveBannerImg: String  //"https://",
  let liveOpenTime: String  // 直播时间
}

class LiveCardView: NibLessView {

  override init(frame: CGRect) {
    super.init(frame: .zero)
    setupUI()
  }

  private let imageView: UIImageView = {
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.contentMode = .scaleAspectFill
    imageView.layer.cornerRadius = 4
    imageView.layer.masksToBounds = true
    imageView.image = UIImage(named: "live_banner")  // 直播图片
    return imageView
  }()

  private let statusView: UIView = {
    let view = UIView()
    view.backgroundColor = .systemRed
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.cornerRadius = 4
    view.layer.masksToBounds = true
    return view
  }()

  private let livingImageView: UIImageView = {
    let img = UIImageView()
    img.translatesAutoresizingMaskIntoConstraints = false
    img.image = UIImage(named: "living.apng")
    return img
  }()

  private let statusLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "直播中"
    label.font = .systemFont(ofSize: 12)
    label.textColor = .white
    label.textAlignment = .center
    return label
  }()

  private let titleLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "热辣滚烫 魅力女神菲尚宠粉专场"
    label.font = .systemFont(ofSize: 16, weight: .medium)
    return label
  }()

  private let timeLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "2023-01-31 19:00:00"
    label.font = .systemFont(ofSize: 12)
    label.textColor = .gray
    return label
  }()

  func setupUI() {
    self.backgroundColor = .white
    constructViewHierarchy()
    activateViewConstraints()
  }

  /// 构建视图层次
  override func constructViewHierarchy() {
    layer.cornerRadius = 8
    layer.masksToBounds = true

    addSubview(imageView)
    addSubview(statusView)
    statusView.addSubview(livingImageView)
    statusView.addSubview(statusLabel)

    addSubview(titleLabel)
    addSubview(timeLabel)
  }

  /// 激活视图约束
  override func activateViewConstraints() {
    NSLayoutConstraint.activate([
      imageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12),
      imageView.topAnchor.constraint(equalTo: topAnchor, constant: 8),
      imageView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8),
      imageView.widthAnchor.constraint(equalToConstant: 70),

      titleLabel.topAnchor.constraint(equalTo: imageView.topAnchor),
      titleLabel.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 8),
      titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10),

      statusView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8),
      statusView.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 8),

      livingImageView.leadingAnchor.constraint(equalTo: statusView.leadingAnchor, constant: 4),
      livingImageView.topAnchor.constraint(equalTo: statusView.topAnchor, constant: 4),
      livingImageView.bottomAnchor.constraint(equalTo: statusView.bottomAnchor, constant: -4),
      livingImageView.widthAnchor.constraint(equalToConstant: 12),
      livingImageView.heightAnchor.constraint(equalToConstant: 12),

      statusLabel.leadingAnchor.constraint(equalTo: livingImageView.trailingAnchor, constant: 4),
      statusLabel.centerYAnchor.constraint(equalTo: statusView.centerYAnchor),
      statusLabel.trailingAnchor.constraint(equalTo: statusView.trailingAnchor, constant: -4),

      timeLabel.centerYAnchor.constraint(equalTo: statusLabel.centerYAnchor),
      timeLabel.leadingAnchor.constraint(equalTo: statusLabel.trailingAnchor, constant: 12),
    ])
  }

  /// 设置 View 样式
  override func setupAppearance() {}

  open func configUI(data: LiveData) {
    if let imageUrl = URL(string: data.liveBannerImg) {
      imageView.kf.setImage(with: imageUrl, placeholder: UIImage(named: "login_image_logo"))
    }
    statusLabel.text = liveStatusString(status: data.liveStatus)  // 1-待开播,2-直播中, 3-已结束,4-已过期
    titleLabel.text = data.liveTopic
    timeLabel.text = data.liveOpenTime
  }

  func liveStatusString(status: Int) -> String? {
    let map = [
      1: "待开播",
      2: "直播中",
      3: "已结束",
      4: "已过期",
    ]
    return map[status]
  }
}
